package com.jrx.anytxn.param.controller.rights;

import com.google.common.collect.Maps;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.rights.PrRightsQualificationReq;
import com.jrx.anytxn.param.dto.rights.PrRightsQualificationRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.rights.IPrRightsQualificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 权益资格参数controller
 *
 * @author lbz
 */
@Api(tags = "权益资格参数API接口")
@RestController
@RequestMapping(value = "/param/rightsQualification")
public class PrRightsQualificationController {


    private static final Logger logger = LoggerFactory.getLogger(PrRightsQualificationController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrRightsQualificationService iPrRightsQualificationService;

    @ApiOperation(value = "根据租户ID和权益ID查询", notes = "根据租户ID和权益ID查询")
    @GetMapping(value = "/{tenantId}/{qualificationId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "qualificationId", value = "权益资格ID", required = true)
    })
    public TxnRespResult<PrRightsQualificationRes> getById(@PathVariable String tenantId,
                                                          @PathVariable String qualificationId) {
        TxnRespResult<PrRightsQualificationRes> result = new TxnRespResult<>();
        PrRightsQualificationRes res;
        try {
            res = iPrRightsQualificationService.getResByTenantIdAndQualificationId(tenantId,qualificationId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增权益资格参数", notes = "新增权益资格参数")
    @PostMapping("/save")
    public TxnRespResult<Integer> add(@RequestBody PrRightsQualificationReq prRightsQualificationReq) {
        TxnRespResult<Integer> result = new TxnRespResult<>();
        Integer res;
        try{
            prRightsQualificationReq.setAllowProducts("");
            prRightsQualificationReq.setLogoUrl("");
            prRightsQualificationReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prRightsQualificationReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrRightsQualificationService.addPrRightsQualification(prRightsQualificationReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询权益资格参数", notes = "分页查询权益资格参数")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrRightsQualificationRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrRightsQualificationRes>> result = new TxnRespResult<>();
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            TxnPage<PrRightsQualificationRes>  res = iPrRightsQualificationService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "更新权益资格参数", notes = "更新权益资格参数")
    @PutMapping("/modify")
    public TxnRespResult<Integer> modifyPrRightsQualification(@RequestBody PrRightsQualificationReq prRightsQualificationReq){
        TxnRespResult<Integer> result = new TxnRespResult<>();
        Integer res;
        try {
            prRightsQualificationReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrRightsQualificationService.modifyPrRightsQualification(prRightsQualificationReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和权益资格ID删除信息", notes = "根据租户ID和权益资格ID删除信息")
    @DeleteMapping(value = "/{tenantId}/{qualificationId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "qualificationId", value = "权益资格ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndQualificationId(@PathVariable String tenantId, @PathVariable String qualificationId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            DeleteLog deleteLog = new DeleteLog(String.valueOf(infoService.getUser().getProjectId()));
            DeleteLogHelper.setDelleteHistory(deleteLog);

            Boolean flag =
                    iPrRightsQualificationService.removeByTenIdAndQualificationId(tenantId, qualificationId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "获得对账文件配置下拉信息", notes = "获得对账文件配置下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PrRightsQualificationRes>>> getList() {
        TxnRespResult<HashMap<String, List<PrRightsQualificationRes>>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<PrRightsQualificationRes> all = iPrRightsQualificationService.findAllByTenId(tenantId);
        HashMap<String, List<PrRightsQualificationRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", all);
        return result.getSuccess(sysCodeMap);
    }

}
